package secondBrush.array;

import java.util.Arrays;

public class E05Leetcoode59 {
    public static int[][] generateMatrix(int n) {
        int setOff = 1;
        int startX = 0;
        int count = 1;
        int startY = 0;
        int loop = 1; //控制循环轮数
        int j,i;

        int[][] result = new int[n][n];
        while (loop <= n / 2) {
            for (  j = startY; j < n - setOff; j++) {
                result[startX][j] = count++;
            }
            for (  i = startX; i < n - setOff; i++) {
                result[i][j] = count++;
            }
            for (; j > startY; j--) {
                result[i][j] = count++;
            }
            for (; i > startX; i--) {
                result[i][j] = count++;
            }


            startX++;
            startY++;
            setOff++;
            loop++;
        }

        if (n % 2 != 0) {
            result[startX][startY] = count;
        }

        return result;

    }

    public static void main(String[] args) {
        int[][] res = generateMatrix(3);
        for (int i = 0; i < res.length; i++) {
            System.out.println();
            for (int j = 0; j < res[i].length; j++) {
                System.out.print(res[i][j]);

            }
        }

    }

}
